import net from "@/common/initiateRequest.js"

exports.install = function(Vue, options) {
	
	Vue.prototype.formatDate = function(fmt, date) {
		let ret;
		let DATE;
		if(!date) return ''
		if (date.toString().length == 10) {
			DATE = new Date(date * 1000)
		} else {
			DATE = new Date(date)
		}
		const opt = {
			"y+": DATE.getFullYear().toString(), // 年
			"m+": (DATE.getMonth() + 1).toString(), // 月
			"d+": DATE.getDate().toString(), // 日
			"H+": DATE.getHours().toString(), // 时
			"M+": DATE.getMinutes().toString(), // 分
			"S+": DATE.getSeconds().toString() // 秒
			// 有其他格式化字符需求可以继续添加，必须转化成字符串
		};
		for (let k in opt) {
			ret = new RegExp("(" + k + ")").exec(fmt);
			if (ret) {
				fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length,
					"0")))
			};
		};
		return fmt;
	};
	
	//type ==0 正常播放
	//type == 1 未播放完整
	//catch 播放失败
	Vue.prototype.playAD = function(adID, type=2) {
		let that = this;
		that.adPromisePushStatus = false;
		that.adPromise;
		that.end = false
		let promise = new Promise(function(resolve, reject) {
			that.adPromise = null
			// js方式仅支持插屏和激励视频广告，其他均采用组件调用
			switch(type) {
				// Banner(组件)
				case 1: 
					that.adPromise = null
					break;
				// 插屏
				case 2: 
					that.adPromise = uni.createInterstitialAd({adUnitId: adID})
					break;
				// 激励视频
				case 3: 
					that.adPromise = wx.createRewardedVideoAd({adUnitId: adID})
					break;
				// 自定义广告
				case 4: 
					that.adPromise = null
					break;
				// 视频广告
				case 5: 
					that.adPromise = null
					break;
				// 体验小程序
				case 6: 
					that.adPromise = null
					break;
			}
			
			uni.showLoading({
				title: '广告加载中...'
			})
			
			that.adPromise.onLoad(() => {
				//设置广告拉取成功
				uni.hideLoading()
				that.adPromisePushStatus = true;
			});
			that.adPromise.onClose((status) => {
				
				if (status && status.isEnded || status === undefined) {
					//正常播放结束,可以下发奖励
					resolve(0)
				} else {
					resolve(1)
					//不下发奖励
					uni.showToast({
						title: '广告未播放完成,无法获取积分'
					})
				}
			});
			that.adPromise.onError((err) => {
				
				uni.hideLoading()
				// uni.showToast({
				// 	icon: "none",
				// 	title: '广告加载失败'
				// })
				reject()
			});
			
			that.adPromise.offError((err) => {
				
			})
			
			showadPromise(that.adPromise)
			setTimeout(() => {
				if (that.adPromisePushStatus === false) {
					that.end = true
					// uni.showToast({
					// 	icon: "none",
					// 	title: '广告加载失败'
					// })
					reject()
				}
			}, 10000)
		})

		function showadPromise(adPromise) {
			if (that.adPromisePushStatus) {
				uni.hideLoading()
				adPromise.show();
			} else {
				if (that.end == true) {
					return
				} else {
					setTimeout(() => {
						showadPromise(adPromise)
					}, 200)
				}

			}
		}
		return promise
	}
	
	// 顶部导航自定义返回处理
	Vue.prototype.back = function() {
		uni.navigateBack({
			delta: 1,
			fail() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			}
		})
	}
	
	// 自定义导航
	Vue.prototype.router = function(url) {
		uni.navigateTo({
			url,
			fail() {
				uni.switchTab({
					url
				})
			}
		})
	}

}
